using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;

using System.Data;
using Prodma.DataAccess.Database;
using Prodma.DataAccess.Models.ViewModels;
using Prodma.DataAccess.DataService;
using Prodma.DataAccess.DataService.ListService;
using Prodma.DataAccess.Helper.Enumerators;
using Prodma.DataAccess.Authorization;
using Prodma.DataAccess.Repositories;
using System.Configuration;
using System.IO;

namespace Prodma.Panel.Controllers
{
    [Authorize]
    public class BannerController : Controller
    {

        #region Repository Setters

        private IBannerRepository repository;

        public BannerController() : this(new BannerService()) { }

        public BannerController(IBannerRepository _repository)
        {
            repository = _repository;
        }

        #endregion


        #region Index
        [NavigationFilter(MenuAdmins.BannerManagement, AuthorizationType.Read)]
        public ActionResult Index()
        {
            ViewData["areaList"] = repository.GetAreaList();

            ViewData["totalRowCount"] = repository.GetTotalRowCount();
            ViewData["bannerCounts"] = repository.GetCounts();

            if (Request.QueryString["find"] != "" && Request.QueryString["find"] != null)
            {
                Dictionary<string, string> find = new Dictionary<string, string>();
                find.Add("name", Request.QueryString["find"]);
                return View(repository.GetList(find));
            }
            else
            {
                return View(repository.GetList());
            }
        }
        #endregion


        #region New
        [NavigationFilter(MenuAdmins.BannerManagement, AuthorizationType.Write)]
        public ActionResult New()
        {
            int area = Convert.ToInt32(Request.QueryString["area"]);

            ViewData["areaList"] = AreaList.GetList();
            ViewData["stateList"] = StateList.GetList(StateType.General);

            System.Web.HttpContext.Current.Session["bannerImage"] = null;

            return View();
        }
        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        [NavigationFilter(MenuAdmins.BannerManagement, AuthorizationType.Write)]
        public ActionResult New(FormCollection form)
        {
            int area = Convert.ToInt32(Request.QueryString["location"]);

            ViewData["projectList"] = ProjectList.GetList();
            ViewData["areaList"] = AreaList.GetList();
            ViewData["stateList"] = StateList.GetList(StateType.General);

            tb_Banner banner = new tb_Banner();
            banner.Name = Request.Form["name"];
            banner.Banner = Request.Form["banner"];
            banner.tb_AreaReference.EntityKey = new EntityKey("DataContext.tb_Area", "Id", Convert.ToInt32(Request.Form["ddlArea"]));
            banner.tb_StateReference.EntityKey = new EntityKey("DataContext.tb_State", "Id", Convert.ToInt32(Request.Form["ddlState"]));

            repository.New(banner);
            return View();
        }
        #endregion


        #region Edit
        [NavigationFilter(MenuAdmins.BannerManagement, AuthorizationType.Update)]
        public ActionResult Edit()
        {
            int id = Convert.ToInt32(Request.QueryString["id"]);
            BannerVm banner = repository.Edit(id);

            int area = Convert.ToInt32(Request.QueryString["area"]);

            ViewData["areaList"] = AreaList.GetList(banner.Area.Id);
            ViewData["stateList"] = StateList.GetList(StateType.General, banner.State.Id);
            ViewData["banner"] = banner.Banner;

            return View(banner);
        }
        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        [NavigationFilter(MenuAdmins.BannerManagement, AuthorizationType.Update)]
        public ActionResult Edit(FormCollection form)
        {
            tb_Banner banner = new tb_Banner();
            banner.Name = Request.Form["name"];
            banner.Banner = Request.Form["banner"];
            banner.tb_AreaReference.EntityKey = new EntityKey("DataContext.tb_Area", "Id", Convert.ToInt32(Request.Form["ddlArea"]));
            banner.tb_StateReference.EntityKey = new EntityKey("DataContext.tb_State", "Id", Convert.ToInt32(Request.Form["ddlState"]));

            int id = Convert.ToInt32(Request.Form["id"]);
            repository.Update(id, banner);

            return RedirectToAction("edit/?id=" + id);
        }
        #endregion


        #region Delete
        [NavigationFilter(MenuAdmins.BannerManagement, AuthorizationType.Delete)]
        public ActionResult Delete()
        {
            string[] idList = Request.QueryString["idList"].Split(',');

            foreach (string id in idList)
            {
                string deleteId = id.Substring(3, id.Length - 3);
                repository.Delete(Convert.ToInt32(deleteId));
            }

            return RedirectToAction("index");
        }
        #endregion


        #region Set Active && Passive
        [NavigationFilter(MenuAdmins.BannerManagement, AuthorizationType.Update)]
        public ActionResult SetActive()
        {
            string[] idList = Request.QueryString["idList"].Split(',');

            foreach (string id in idList)
            {
                string currentId = id.Substring(3, id.Length - 3);
                repository.SetActive(Convert.ToInt32(currentId));
            }

            return RedirectToAction("index");
        }
        [NavigationFilter(MenuAdmins.BannerManagement, AuthorizationType.Update)]
        public ActionResult SetPassive()
        {
            string[] idList = Request.QueryString["idList"].Split(',');

            foreach (string id in idList)
            {
                string currentId = id.Substring(3, id.Length - 3);
                repository.SetPassive(Convert.ToInt32(currentId));
            }

            return RedirectToAction("index");
        }
        #endregion


        #region Order Operation

        public ActionResult OrderUp()
        {
            int bannerId = Convert.ToInt32(Request.QueryString["id"]);
            int orderNo = Convert.ToInt32(Request.QueryString["orderNo"]);
            int area = Convert.ToInt32(Request.QueryString["area"]);

            BannerOrderVm bannerOrder = new BannerOrderVm();
            bannerOrder.Banner = bannerId;
            bannerOrder.OrderNo = orderNo;
            bannerOrder.Area = area;

            repository.OrderUp(bannerId, bannerOrder);

            return RedirectToAction("index");
        }

        public ActionResult OrderDown()
        {
            int bannerId = Convert.ToInt32(Request.QueryString["id"]);
            int orderNo = Convert.ToInt32(Request.QueryString["orderNo"]);
            int area = Convert.ToInt32(Request.QueryString["area"]);

            BannerOrderVm bannerOrder = new BannerOrderVm();
            bannerOrder.Banner = bannerId;
            bannerOrder.OrderNo = orderNo;
            bannerOrder.Area = area;

            repository.OrderDown(bannerId, bannerOrder);

            return RedirectToAction("index");
        }

        public ActionResult SetOrder()
        {
            int bannerId = Convert.ToInt32(Request.QueryString["id"]);
            int orderNo = Convert.ToInt32(Request.QueryString["orderNo"]);
            int area = Convert.ToInt32(Request.QueryString["area"]);
            int setOrderNo = Convert.ToInt32(Request.QueryString["setOrderNo"]);

            if (setOrderNo < 0)
            {
                return RedirectToAction("/index/?area=" + Request.QueryString["area"]);
            }

            BannerOrderVm bannerOrder = new BannerOrderVm();
            bannerOrder.Banner = bannerId;
            bannerOrder.OrderNo = orderNo;
            bannerOrder.Area = area;
            bannerOrder.SetOrderNo = setOrderNo;

            repository.SetOrder(bannerId, bannerOrder);

            return RedirectToAction("index");
        }
        #endregion

    }
}
